<#if console.log == true >
var log = console.log.bind(console);
<#else>
var log = function () { }; 
</#if>

<#if console.info == true >
var info = console.info.bind(console); 
<#else>
var info = function () { };
</#if>

<#if console.error == true >
var error = console.error.bind(console); 
<#else>
var error = function () { }; 
</#if>



var _appcode_start = 1000;
var appcode = {
    NOTLOGIN : 1 + _appcode_start,  //没有登录
    TIMEOUT : 2 + _appcode_start,  //登录超时
    KICKTED : 3 + _appcode_start,  // 帐号在其它地方登录
    NOTPERMISSION : 4 + _appcode_start,//登录了，但是没有权限操作
    REFUSE : 5 + _appcode_start,//拒绝访问
    NEED_ACCESS_TOKEN : 6 + _appcode_start,//需要提供正确的access_token
    CAPTCHA_ERROR : 7 + _appcode_start // 图形验证码错误code
}; 


var tEifosafjieo = "oxjyj/8aklao/uijkkjujkdfla/34g787doperjglnsafhxikjy--";
// var tesoegEgac1 = "t9P8i6J9o";
var tsvtesfvcomparedcode = 996;//1006

/**
 * @param {*} source 
 * @param {*} destination
 */
var copy = function (destination, source) {
    tEifosafjieo += "";
    tesoegEgac += "";
    //destination, source
    if (source && destination) {
        for (var propertyName in source) {
            destination[propertyName] = source[propertyName];
        }
    }
};

// 获取浏览器窗口的可视区域的宽度
function getViewPortWidth() {
    return document.documentElement.clientWidth || document.body.clientWidth;
}
 
// 获取浏览器窗口的可视区域的高度
function getViewPortHeight() {
    return document.documentElement.clientHeight || document.body.clientHeight;
}
 
// 获取浏览器窗口水平滚动条的位置
function getScrollLeft() {
    return document.documentElement.scrollLeft || document.body.scrollLeft;
}
 
// 获取浏览器窗口垂直滚动条的位置
function getScrollTop() {
    return document.documentElement.scrollTop || document.body.scrollTop;
}

/**
 * 深度拷贝，
 * @param {*} obj 
 */
function deepCopy(obj) {
    var c = obj instanceof Array ? [] : {};
    for (var i in obj) if (obj.hasOwnProperty(i)) {
        var prop = obj[i];
        if (typeof prop == 'object') {
            if (prop instanceof Array) {
                c[i] = [];
                for (var j = 0; j < prop.length; j++) {
                    if (typeof prop[j] != 'object') {
                        c[i].push(prop[j]);
                    } else {
                        c[i].push(deepCopy(prop[j]));
                    }
                }
            } else {
                c[i] = deepCopy(prop);
            }
        } else {
            c[i] = prop;
        }
    }
    return c;
}

var inittesoegEgac = function() {
    //取tPiJo
    // t9P8i6J9o
    tesoegEgac = 'tio_access_token';//tesoegEgac1.substr(0, 1) + tesoegEgac1.substr(2, 1) + tesoegEgac1.substr(4, 1) + tesoegEgac1.substr(6, 1) + tesoegEgac1.substr(8, 1);
    tsvtesfvcomparedcode += 10;
}
inittesoegEgac();
/**
 * 获取url的参数值
 * @param {*} name 
 */
function getQueryString(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        var v = r[2];
        return decodeURIComponent(v);
    }
    return null;
}

/**
 * 获取相对路径
 * 例如： http//www.liangshunet.com/pub/item.aspx?t=osw7，则截取到的相对路径为：/pub/item.aspx
 */
function getRequestPath() {
    var url = document.location.toString();
    var arrUrl = url.split("//");
    var start = arrUrl[1].indexOf("/");
    var relUrl = arrUrl[1].substring(start); //stop省略，截取从start开始到结尾的所有字符
    if (relUrl.indexOf("?") != -1) {
        relUrl = relUrl.split("?")[0];
    }
    return relUrl;
}
/**
 * 随机生成字符串（当前主要用于生成校验码）
 * @param {*} len 
 */
function randomString(len) {
    len = len || 32;
    var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
    var maxPos = $chars.length;
    var pwd = '';
    for (i = 0; i < len; i++) {
        pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
    }
    return pwd;
}


// var key = CryptoJS.enc.Utf8.parse("uPezilSoTLyzklys");
/**
 * [encrypt 加密]
 * @return {[type]} [description]
 */
function aes_encrypt(content, key) {
    var encryptResult = CryptoJS.AES.encrypt(content, key, {
        iv: key,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    return encryptResult;
}

/**
 * [decrypt 解密]
 * @return {[type]} [description]
 */
function aes_decrypt(content, key) {
    var bytes = CryptoJS.AES.decrypt(content.toString(), key, {
        iv: key,
        mode: CryptoJS.mode.CBC,
        padding: CryptoJS.pad.Pkcs7
    });
    var decryptResult = bytes.toString(CryptoJS.enc.Utf8);
    return decryptResult;
}

/**
 * ajax.get("/user/curr", {
 *      success:function(resp){
 *          
 *      }
 * });
 * 
 * ajax.post("/xx/create", {
 *      success:function(resp){
 *          
 *      }
 * });
 */
var ajax = {
    mysuccess: function (method, url, conf, success) {
        this.h = function (resp) {
            if (resp && resp.ok == false) {
                log(method + " " + url + ": 业务上失败, 请求: " + JSON.stringify(conf) + ", 响应: " + JSON.stringify(resp));
                var code = resp.code;
                if (code == tsvtesfvcomparedcode) {
                    var s1_r = randomString(9);
                    var s1_t = new Date().getTime();
                    var key1 = "UijSolys";
                    var xxx = "$" + "{" + "" + key1 + s1_r + s1_t + "}";
                    var s1_s = $.md5(xxx);

                    var step1path = tEifosafjieo.substr(5, 1) + tEifosafjieo.substr(7, 1) + tEifosafjieo.substr(5, 1) + tEifosafjieo.substr(1, 1);// 取/a/x
                    ajax.get(step1path, {
                        async: false,  //同步发送
                        data: { r: s1_r, t: s1_t, s: s1_s },
                        success: function (resp) {
                            if (resp.ok) {
                                //x、y、 z、i、t
                                var data2 = resp.data;
                                var x = data2.x;
                                var y = data2.y;
                                var z = data2.z;
                                var i = data2.i;
                                var t = new Date().getTime();
                                var key2 = "PkjSmlys";
                                var s = $.md5("$" + "{" + key2 + x + z + "}");
                                var step2path = tEifosafjieo.substr(5, 1) + tEifosafjieo.substr(7, 1) + tEifosafjieo.substr(5, 1) + tEifosafjieo.substr(3, 1);// 取/a/y
                                ajax.get(step2path, {
                                    async: false,  //同步发送
                                    data: { x: y, y: x, z: i, i: z, t: t, s: s },
                                    success: function (resp) {
                                        if (resp.ok) {
                                            var kkeeyy = CryptoJS.enc.Utf8.parse("uPezilSoTLyzklys");
                                            var xxyy = aes_decrypt(resp.data, kkeeyy);
                                            tio.cookie.set(tesoegEgac, xxyy, { expires: 1 / 12 });
                                            window.location.reload();
                                        }
                                    }
                                });
                            }
                        }
                    });
                } else {
                    if (code == appcode.NOTLOGIN || code == appcode.TIMEOUT || code == appcode.KICKTED) {
                        if (curruser) {
                            location.reload();
                        } else {
                            try {
                                tiosite_show_login(1);
                            } catch (error) {
                                
                            }
                            
                        }
                        return;
                    } else if (code == appcode.REFUSE) {
                        //弹一下框，提示用户拒绝的原因
                        if (!isBlank(resp.msg)) {
                            layer.msg(resp.msg)
                        } else {
                            layer.msg("拒绝访问")
                        }
                        return;
                    }
                }

            } else {
                log(method + " " + url + ": 业务上成功, 请求: " + JSON.stringify(conf) + ", 响应: " + JSON.stringify(resp));
            }

            // var args = [];
            // for (var i = 1; i < arguments.length; i++) {
            //     args[i - 1] = arguments[i];
            // }
            // log("ddddddddddddd", resp, args, arguments);

            success.apply(success, arguments);
        }
    },

    _: function (method, url, conf) {
        var _conf = {

            dataType: 'json', // 服务器返回json格式数据
            type: method,    // HTTP请求方法
            timeout: 50000 // 请求时间设置为50秒；
        };

        var xx = new ajax.mysuccess(method, url, conf, conf.success);
        
        if (conf.success) {
            conf.success = xx.h;
        }


        //destination, source
        copy(_conf, conf);


        url = path(url);

        $.ajax(url, _conf);
    },
    get: function (url, conf) {
        ajax._('get', url, conf);
    },
    post: function (url, conf) {
        ajax._('post', url, conf);
    }
}

/**
 * 该方法可以修改url的参数。
例如将
　　t-io.org
修改为
　　t-io.org?name=123
操作为：
　　window.location.href = changeURLArg(window.location.href,'name',123)
 * @param {*} url 
 * @param {*} arg 
 * @param {*} arg_val 
 */
function changeURLArg(url, arg, arg_val) {
    var pattern = arg + '=([^&]*)';
    var replaceText = arg + '=' + arg_val;
    if (url.match(pattern)) {
        var tmp = '/(' + arg + '=)([^&]*)/gi';
        tmp = url.replace(eval(tmp), replaceText);
        return tmp;
    } else {
        if (url.match('[\?]')) {
            return url + '&' + replaceText;
        } else {
            return url + '?' + replaceText;
        }
    }
}

/**
 * 数组去重
 * @param {} array 
 */
function array_unique(array) {
    // res用来存储结果
    var res = [];
    for (var i = 0, arrayLen = array.length; i < arrayLen; i++) {
        for (var j = 0, resLen = res.length; j < resLen; j++ ) {
            if (array[i] === res[j]) {
                break;
            }
        }
        // 如果array[i]是唯一的，那么执行完循环，j等于resLen
        if (j === resLen) {
            res.push(array[i])
        }
    }
    return res;
}



 var isBlank = function (value) {
    if (value === null || value === undefined || $.trim(value) === "") {
        return true;
    }
    return false;
};




function number_format(number, decimals, dec_point, thousands_sep) {
    /*
    * 参数说明：
    * number：要格式化的数字
    * decimals：保留几位小数
    * dec_point：小数点符号
    * thousands_sep：千分位符号
    * */
    number = (number + '').replace(/[^0-9+-Ee.]/g, '');
    var n = !isFinite(+number) ? 0 : +number,
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.ceil(n * k) / k;
        };
 
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    var re = /(-?\d+)(\d{3})/;
    while (re.test(s[0])) {
        s[0] = s[0].replace(re, "$1" + sep + "$2");
    }
 
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}